package novel.spider.impl.novel;

import novel.spider.NovelSiteEnum;
import novel.spider.entitys.Novel;
import novel.spider.util.NovelSpiderUtil;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * 看书中小说列表抓取
 * Created by Kau on 2016/11/5.
 */
public class KanShuZhongNovelSpider extends AbstractNovelSpider {
    public KanShuZhongNovelSpider() {
    }

    public List<Novel> GetsNovel(String url,Integer maxTryTimes){
        List<Novel> novels = new ArrayList<>();
        try {
            Elements trs = super.getsTr(url,maxTryTimes);
            for (int index = 1, size = trs.size() - 1; index < size; index++) {
                Elements tds = trs.get(index).getElementsByTag("td");
                Novel novel = new Novel();
                novel.setType(tds.first().text());
                novel.setName(tds.get(1).text());
                novel.setURL(tds.get(1).getElementsByTag("a").first().absUrl("href"));
                novel.setLastUpdateChapter(tds.get(2).text());
                novel.setLastUpdateChapterURL(tds.get(2).getElementsByTag("a").first().absUrl("href"));
                novel.setAuthor(tds.get(3).text());
                novel.setLastUpadteTime(NovelSpiderUtil.getDate(tds.get(4).text(), "MM-dd"));
                novel.setStatus(NovelSpiderUtil.getNovelStatus(tds.get(5).text()));
                novel.setPlatformId(NovelSiteEnum.GetEnumByUrl(url).getId());
                novels.add(novel);
            }

        } catch (Exception e){
            throw new RuntimeException(e);
        }
        return novels;
    }


}